package com.kye.express.business.personal_my_bill.monthly_balance_bill.list;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.RequestUser;
import com.kye.model.bean.bill.MonthlyBalanceBillBean;
import com.kye.model.bean.user.User;
import com.kye.model.service.BillService;

import java.util.List;

import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public interface MyBillListContract {
    interface MyBillListView extends IView, LoadingView, ErrorView {

        void loadedData(List<MonthlyBalanceBillBean> billBeans);

        void onEmpty(String msg);
    }

    class MyBillListPresenter extends BasePresenter<MyBillListView> {
        public void loadData() {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, SingleSource<OpenApiResult<List<MonthlyBalanceBillBean>>>>() {
                        @Override
                        public SingleSource<OpenApiResult<List<MonthlyBalanceBillBean>>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(BillService.class)
                                    .getMonthlyBalanceBillList(new RequestUser(user.getPhone()));
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<List<MonthlyBalanceBillBean>>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<List<MonthlyBalanceBillBean>> result, List<MonthlyBalanceBillBean> billBeans) {
                            if (35223 == result.getCode()) {
                                getView().onEmpty(result.getMsg());
                            }
                            getView().loadedData(billBeans);
                        }

                        @Override
                        public void onApiError(OpenApiResult<List<MonthlyBalanceBillBean>> result, int code, String msg) {
                            getView().onError(msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }
    }
}
